/**
 * 
 */

function XMLTTemplate(templateString) {
	
	var templateDoc = null;
	var params = null;
	var sections = null;

	try {
		templateDoc = $.parseXML(templateString);
		params = [];
		sections = [];
		$.each($(templateDoc).find('template').children('parameter'), 
				function(index, value) {
					params.push(new XMLTTemplateParam(value))
				});
		$.each($(templateDoc).find('section'), 
				function(index, value) {
					sections.push(new XMLTTemplateSection(value));
				});
	} catch (e) {
		alert('Error parsing template: ' + e.message);
	}
	
	this.render = function() {
		// render template into element
		this.renderParameters();
		for (var i in sections) {
			var s = sections[i];
			if (!s) {
				continue;
			}
			s.render('sectionsdiv');
		}
	}
	
	this.renderParameters = function() {
		$('#params_tbody').html('');
		for (var i in params) {
			var p = params[i];
			if (!p) {
				continue;
			}
			p.render('params_tbody');
		}
	}
	
	this.addParam = function() {
		var namesMap = [];
		for (var i in params) {
			var p = params[i];
			if(!p)
				continue;
			
			namesMap[p.getName()] = true;
		}
		var suffix = 1;
		while(namesMap['param' + suffix])
			suffix++;
		
		var paramEl = templateDoc.createElement('parameter');
		templateDoc.documentElement.appendChild(paramEl);
		var $el = $(paramEl);
		$el.attr('name', 'param' + suffix);
		$el.attr('multiple', 'false');
		$el.attr('type', 'value');
		$el.attr('ref', '');
		
		var p = new XMLTTemplateParam(paramEl);
		params.push(p);
		
		p.render('params_tbody');
		
		setDirtyFlag(true);
		buildParamTree();
	}
	
	this.delParam = function(name, el) {
		if(!name)
			return;
		
		for (var i in params) {
			var p = params[i];
			if(!p)
				continue;
			
			if (p.getName() == name)
				delete params[i];
		}
		$(el).closest('tr').remove();
		setDirtyFlag(true);
		buildParamTree();
	}
	
	this.getParamByName = function(name) {
		for (var i in params) {
			var p = params[i];
			if(!p)
				continue;
			
			if (p.getName() == name)
				return p;
		}
		return null;
	}
	
	this.addSection = function() {
		var namesMap = [];
		for (var i in sections) {
			var s = sections[i];
			if(!s)
				continue;
			
			namesMap[s.getName()] = true;
		}
		var suffix = 1;
		while(namesMap['section' + suffix])
			suffix++;
		
		var sectionEl = templateDoc.createElement('section');
		templateDoc.documentElement.appendChild(sectionEl);
		sectionEl.setAttribute('name', 'section' + suffix);
		sectionEl.appendChild(templateDoc.createElement('body'));
		
		var s = new XMLTTemplateSection(sectionEl);
		sections.push(s);
		
		s.render('sectionsdiv');
		
		setDirtyFlag(true);
	}
	
	this.getSectionByName = function(name) {
		for (var i in sections) {
			var s = sections[i];
			if(!s)
				continue;
			
			if (s.getName() == name)
				return s;
		}
		return null;
	}
	
	this.getString = function() {
		// return template XML string
		var str = '<?xml version="1.0" encoding="UTF-8"?><template>';
		for (var i in params) {
			var p = params[i];
			if(!p)
				continue;
			
			str += p.getString();
		}
		$.each($('#generatorsdiv').find('SELECT'), 
				function(index, value) {
					str += '<generator id="' + $(value).val() + '" />';
				});
		for (var i in sections) {
			var s = sections[i];
			if(!s)
				continue;
			
			str += s.getString();
		}
		str += '</template>';
		return str;
	}
}
